var MyTable={
    searchForm:"searchForm",
    searchUrl:"/menu/list",
	tableId:"lgTable",
	prefix:"/menu/",
	table:null,
};
$(function() {
	loadTreeData();
});

function add(id) {
	layer.open({
		type : 2,
		title : '创建菜单',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '560px' ],
		content : MyTable.prefix + '/add/'+id
	});
}

function edit(menuId) {
	layer.open({
		type : 2,
		title : '修改菜单',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '560px' ],
		content : MyTable.prefix+'/edit/'+menuId
	});
}

function loadTreeData() {
	EasyAjax.post({url : "/menu/selectTreeList"},function(r){
		var res=[];
		res.push(r);
		loadTree(res);
	});
}



function deleteRow(menuId) {
    var config={
			url : MyTable.prefix+"/deleteRow",
			data:{"menuId":menuId},
			sureDo:true,
			msg:"确定删除记录？"
	}
	EasyAjax.post_json(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			location.reload();
		} 
	});
}

function loadTree(r){
	layui.use(['form','tree', 'layer'], function() {
		var layer = layui.layer, form = layui.form, $ = layui.jquery;
	    var layTree=layui.treeGird({
	        elem:'#lgTable',
	        spreadable: false, //设置是否全展开，默认不展开
	        checkbox : false,
	        nodes:r,
	        layout: layout
	    });
	    /*$('#collapse').on('click', function() {
	        layui.collapse(layTree);
	    });
	    $('#expand').on('click', function() {
	        layui.expand(layTree);
	    });*/
	    form.render();
	});
}

var layout = [
    { name: '菜单名称', treeNodes: true, headerClass: 'value_col', colClass: 'value_col', style: 'width: 20%' },
    { name: '菜单地址', treeNodes: false, headerClass: 'value_col', colClass: 'value_col', style: 'width: 20%',
    	render: function(row) {
    		if(row.attributes&&row.attributes.url){
    			 return '<span>'+row.attributes.url+'</span>'; //列渲染
    		}else{
    			return "无";
    		}
        }
    },
    { name: '权限标识', treeNodes: false, headerClass: 'value_col', colClass: 'value_col', style: 'width: 15%',
    	render: function(row) {
    		if(row.attributes&&row.attributes.perms){
    			return '<span>'+row.attributes.perms+'</span>'; //列渲染
    		}else{
    			return "无";
    		}
        }
    },
    { name: '菜单类型', treeNodes: false, headerClass: 'value_col', colClass: 'value_col', style: 'width: 10%',
    	render: function(row) {
    		if(row.attributes){
    			    var css=""
    	    		switch (row.attributes.type) {
    	    		case 0:
    	    			type="目录";
    				    css="label label-primary";
      					break;
                    case 1:
                    	type="菜单";
                    	css="label label-warning";
    					break;
                    case 2:
                    	type="按钮";
                    	css="label label-success";
    					break;
    				default:
    					break;
    				}
                    return '<span class="'+css+'">'+type+'</span>'; //列渲染
    		}else{
    			return "";
    		}
        }
    },
    { name: '菜单图标', treeNodes: false, headerClass: 'value_col', colClass: 'value_col', style: 'width: 10%',
	    render: function(row) {
	    	if(row.attributes&&row.attributes.icon){
	    		return '<a class="btn btn-primary btn-sm " href="#"><i class="'+row.attributes.icon+'"></i></a>';
    		}else{
    			return "";
    		}
        }
    },
    {
        name: '操作',
        headerClass: 'value_col',
        colClass: 'value_col',
        style: 'width: 20%;text-align: center;',
        render: function(row) {
        	var e = '<a  class="btn btn-primary btn-sm ' + editBtn + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
			+ row.id
			+ '\')"><i class="fa fa-edit "></i></a> ';
		    var d = '<a class="btn btn-warning btn-sm ' + delBtn + '" href="#" title="删除"  mce_href="#" onclick="deleteRow(\''
			+ row.id
			+ '\')"><i class="fa fa-remove"></i></a> ';
		    var a = '<a class="btn btn-primary  btn-sm ' + addBtn + '" href="#" title="新增"  mce_href="#" onclick="add(\''
			+ row.id
			+ '\')"><i class="fa fa-plus"></i></a> ';
		    if(row.id==-1){
		    	 return "";	
		    }
		    if(row.attributes){
		    	var type=row.attributes.type;
		    	if(type==0||type==1){
		    		return e + d+ a;
		    	}else{
		    		return e + d;
		    	}
		    }else{
		    	return "";
		    }
        }
    },
];
